package com.atguigu.stream;

import com.atguigu.java2.Teacher;
import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;

public class TeacherTest {

    /**
     * https://it.cha138.com/php/show-59417.html
     * 选择占比最大的排序(考虑权重)
     */
    @Test
    public void getMaxSortWithWeight() {
        List<Teacher> teachers = TeacherData.getTeachersWithSameCupNo();
        //先分组
        Map<String, List<Teacher>> collect = teachers.stream().collect(Collectors.groupingBy(Teacher::getSort));
        String sort = "";
        double result = 0.00d;
        for (Map.Entry<String, List<Teacher>> stringListEntry : collect.entrySet()) {
            List<Teacher> teacherList = stringListEntry.getValue();
            double sum = 0.00d;
            for (Teacher teacher : teacherList) {
                sum += Double.parseDouble(teacher.getWeight());
            }
            if (sum >= result) {
                result = sum;
                sort = stringListEntry.getKey();
            }
        }
        System.out.println("result = " + result);
        System.out.println("sort = " + sort);
    }

    /**
     * https://it.cha138.com/php/show-59417.html
     * 选择占比最大的排序(不考虑权重)
     */
    @Test
    public void getMaxSortWithoutWeight() {
        List<Teacher> teachers = TeacherData.getTeachersWithDifferentCupNo();
        //根据杯号分组
        Map<String, List<Teacher>> collect = teachers.stream().collect(Collectors.groupingBy(Teacher::getCupNo));
        Map<Integer, Integer> treeMap = new TreeMap<>();
        //获取所有的杯号及总排序
        List<SortInfo> sortInfos = new ArrayList<>();
        for (Map.Entry<String, List<Teacher>> stringListEntry : collect.entrySet()) {
            SortInfo sortInfo = new SortInfo();
            List<Teacher> teacherList = stringListEntry.getValue();
            Integer sum = 0;
            for (Teacher teacher : teacherList) {
                sum += Integer.valueOf("".equals(teacher.getSort()) ? "0" : teacher.getSort());
            }
            treeMap.put(Integer.valueOf(stringListEntry.getKey()), sum);
            sortInfo.setCupNo(stringListEntry.getKey());
            sortInfo.setSumSort(sum);
            sortInfos.add(sortInfo);
        }
        System.out.println("treeMap = " + treeMap);
        List<SortInfo> sortInfos1 =
                sortInfos.stream().sorted(Comparator.comparing(SortInfo::getSumSort)).collect(Collectors.toList());
        System.out.println(sortInfos1);
        StringBuffer sb = new StringBuffer("");
        for (int i = 0; i < sortInfos1.size(); i++) {
            //由于根据汇总排序字段从小到大排过序了，所以只会存在小于等于的情况。
            if (i <= (sortInfos1.size() - 2)) {
                if (sortInfos1.get(i).getSumSort() < sortInfos1.get(i + 1).getSumSort()) {
                    sb.append(sortInfos1.get(i).getCupNo() + ">");
                    if (null == sortInfos1.get(i).getFinalSort()) {
                        sortInfos1.get(i).setFinalSort(i + 1);
                    }
                    if (null == sortInfos1.get(i + 1).getFinalSort()) {
                        sortInfos1.get(i + 1).setFinalSort(sortInfos1.get(i).getFinalSort() + 1);
                    }

                } else if (sortInfos1.get(i).getSumSort().equals(sortInfos1.get(i + 1).getSumSort())) {
                    sb.append(sortInfos1.get(i).getCupNo() + "=");
                    if (null == sortInfos1.get(i).getFinalSort()) {
                        sortInfos1.get(i).setFinalSort(i + 1);
                    }
                    if (null == sortInfos1.get(i + 1).getFinalSort()) {
                        sortInfos1.get(i + 1).setFinalSort(sortInfos1.get(i).getFinalSort());
                    }
                }
            } else {
                //最后一个元素
                sb.append(sortInfos1.get(i).getCupNo());
                if (sortInfos1.get(i).getSumSort() < sortInfos1.get(i - 1).getSumSort()) {
                    sortInfos1.get(i).setFinalSort(sortInfos1.get(i - 1).getFinalSort() + 1);
                } else if (sortInfos1.get(i).getSumSort().equals(sortInfos1.get(i - 1).getSumSort())) {
                    sortInfos1.get(i).setFinalSort(sortInfos1.get(i - 1).getFinalSort());
                }

            }

        }
        System.out.println(sb);
        System.out.println(sortInfos1);
    }
}
